package cn.hpclub.server.controller.h5;

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import cn.hpclub.server.constant.ClientApiConstant;
import cn.hpclub.server.model.Opportunity;

public class OpportunityH5Controller extends BaseH5Controller{

    private static final Logger logger = LoggerFactory.getLogger(OpportunityH5Controller.class);

    public void getOpportunityList(){
        int pageNumber = getParaToInt("pageNumber", 1);
        int pageSize = getParaToInt("pageSize", 10);
        String PRE_BUSI_ID = getPara("PRE_BUSI_ID");
        String PRE_BUSI_NAME = getPara("PRE_BUSI_NAME");
        Page<Record> pager = Opportunity.dao.getList(pageNumber, pageSize, PRE_BUSI_ID, PRE_BUSI_NAME);
        renderJson(pager);
    }

    public void saveOpportunity(){
        JSONObject jsonObject = setResponseObj(ClientApiConstant.ResultCode.FAILED, ClientApiConstant.Msg.FAIL,
                                               ClientApiConstant.Msg.EMPTY);
        Opportunity obj = new Opportunity();
        try{
            obj.set(Opportunity.PRE_BUSI_ID, getPara(Opportunity.PRE_BUSI_ID, null));
            obj.set(Opportunity.PRE_BUSI_NAME, getPara(Opportunity.PRE_BUSI_NAME, null));
            obj.set(Opportunity.CUST_GRP_NAME, getPara(Opportunity.CUST_GRP_NAME, null));
            obj.set(Opportunity.CUST_CONT_NAME, getPara(Opportunity.CUST_CONT_NAME, null));
            obj.set(Opportunity.CUST_CONT_PHONE, getPara(Opportunity.CUST_CONT_PHONE, null));
            obj.set(Opportunity.CUST_CONT_TEL, getPara(Opportunity.CUST_CONT_TEL, null));
            obj.set(Opportunity.CUST_CONT_ADDR, getPara(Opportunity.CUST_CONT_ADDR, null));
            obj.set(Opportunity.CITY_GROUP_ID, getPara(Opportunity.CITY_GROUP_ID, null));
            obj.set(Opportunity.CITY_GROUP_NAME, getPara(Opportunity.CITY_GROUP_NAME, null));
            obj.set(Opportunity.COUNTY_GROUP_ID, getPara(Opportunity.COUNTY_GROUP_ID, null));
            obj.set(Opportunity.COUNTY_GROUP_NAME, getPara(Opportunity.COUNTY_GROUP_NAME, null));
            obj.set(Opportunity.BUSI_TYPE, getPara(Opportunity.BUSI_TYPE, null));
            obj.set(Opportunity.BUSI_CLASS, getPara(Opportunity.BUSI_CLASS, null));
            obj.set(Opportunity.BUSI_TYPE_NAME, getPara(Opportunity.BUSI_TYPE_NAME, null));
            obj.set(Opportunity.BUSI_CLASS_NAME, getPara(Opportunity.BUSI_CLASS_NAME, null));
            obj.set(Opportunity.BUSI_REQ_DESC, getPara(Opportunity.BUSI_REQ_DESC, null));
            obj.set(Opportunity.PRE_CALL_DAY, getPara(Opportunity.PRE_CALL_DAY, null));
            obj.set(Opportunity.PRE_CALL_TIME, getPara(Opportunity.PRE_CALL_TIME, null));
            obj.set(Opportunity.CALL_LIMIT_DAY, getPara(Opportunity.CALL_LIMIT_DAY, null));
            obj.set(Opportunity.CHAN_ID, getPara(Opportunity.CHAN_ID, null));
            obj.set(Opportunity.REMARK, getPara(Opportunity.REMARK, null));
            obj.set(Opportunity.UNIT_ID, getPara(Opportunity.UNIT_ID, null));
            obj.set(Opportunity.ALL_MOBILE_USER_TOTOAL, getPara(Opportunity.ALL_MOBILE_USER_TOTOAL, null));
            obj.set(Opportunity.CHINA_MOBLIE_USER_TOTOAL, getPara(Opportunity.CHINA_MOBLIE_USER_TOTOAL, null));
            if(obj.save()){
                String visitId = String.valueOf(obj.getInt(Opportunity.ID));
                logger.debug("save Opportunity success,primary Id=" + visitId);
                jsonObject = setResponseObj(ClientApiConstant.ResultCode.SUCCESS_CODE, "save Opportunity success",
                                            ClientApiConstant.Msg.EMPTY);
            }
        }
        catch(Exception e){
            e.printStackTrace();
            jsonObject = setResponseObj(ClientApiConstant.ResultCode.PARAM_ERROR, e.getMessage(),
                                        ClientApiConstant.Msg.EMPTY);
        }
        renderJson(jsonObject.toString());

    }

}
